﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WsdShop.Model;
using WsdShop.IService;
using Infrastructure.Pub;
using Infrastructure.Tool;
using SqlSugar;
using Infrastructure;
using System.Text;

namespace WeChat
{
    [AuthorizeFilterAttribute]
    public class BaseController : Controller
    {
        private Member _menber;

        public IMemberService _memberService;

        public Member CurrentLoginUser
        {
            get
            {
                return _menber;
            }
        }

        public BaseController(IMemberService s)
        {
            _memberService = s;
            string uniqueKey = PubGet.GetMemberKey;
            if (!uniqueKey.IsNullOrEmpty())
            {
                _menber = CacheManager<Member>.GetInstance()[uniqueKey];
                if (!IsLogin)
                {
                    _menber = _memberService.GetMemberInfo((Generator.GetCookie(PubConst.CookieMemberKey)).ObjToInt());
                    CacheManager<Member>.GetInstance().Add(uniqueKey, _menber, 1200);
                }
            }
           

        }


        public bool IsLogin
        {
            get
            {
                return (_menber != null && _menber.Id > 0);
            }
        }


        protected override JsonResult Json(object data, string contentType, Encoding contentEncoding)
        {
            return new CustomJsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding };
        }

        public new JsonResult Json(object data, JsonRequestBehavior jsonRequest)
        {
            return new CustomJsonResult { Data = data, JsonRequestBehavior = jsonRequest };
        }

        public new JsonResult Json(object data)
        {
            return new CustomJsonResult { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
    }


    public class MyLoginAuthorize : AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            string mid = PubGet.GetMemberId;
            if (!mid.IsNullOrEmpty())
            {
                return;
            }
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                if (mid.IsNullOrEmpty())
                {

                    Result result = new Result();
                    result.msg = "不好意思,登录超时,请刷新页面再操作";
                    filterContext.HttpContext.Response.Write(SqlSugar.JsonConverter.Serialize(result));
                    filterContext.HttpContext.Response.End();
                    
                }
            }
            if (filterContext.HttpContext.Request.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase))
            {
                if (mid.IsNullOrEmpty())
                {
                    Result result = new Result();
                    result.msg = "不好意思,登录超时,请刷新页面再操作!";
                    filterContext.HttpContext.Response.Write(SqlSugar.JsonConverter.Serialize(result));
                    filterContext.HttpContext.Response.End();
                }
            }
            base.HandleUnauthorizedRequest(filterContext);
        }

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (PubGet.GetMemberId.IsNullOrEmpty() && !httpContext.Request.IsAjaxRequest())
            {

                // 未登录跳转
                //httpContext.Response.Redirect("/Wx/Index?returnUrl=" + httpContext.Request.Url);
                //测试
                httpContext.Response.Redirect("/Wx/Callback?returnUrl=" + httpContext.Request.Url);
            }
            return base.AuthorizeCore(httpContext);
        }

    }
}